/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Asko
 */
public class DBOutputStream extends OutputStream {

    private final String INSERT_TO_EXP =
            "insert into experiments values (null,? , ? )";
    private final String INSERT_TO_EXP_DATA = "insert into exp_data values(null,"
            + "?,?,?)";
    private int counter = 0;
    private int index_cnt = 1;
    private static final int PACKET_LENGTH = 1024 * 500;
    private byte[] buf = new byte[PACKET_LENGTH];
    private Connection con;
    
    private int serID;
   

    public DBOutputStream(Connection con, Experiment exp) throws SQLException {
        this.con = con;
        java.util.Date udate = new java.util.Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String res = df.format(udate);
        PreparedStatement smnt = con.prepareStatement(INSERT_TO_EXP,
                PreparedStatement.RETURN_GENERATED_KEYS);
        smnt.setString(1, res);
        smnt.setString(2, exp.getExpName());
        smnt.executeUpdate();
        ResultSet rs = smnt.getGeneratedKeys();
        rs.next();
        serID = rs.getInt(1);

    }

    @Override
    public void close() throws IOException {
        sendLastBuf();
        super.close();
    }

    @Override
    public void write(int b) throws IOException {
        try {
            buf[counter++] = (byte) b;
         
            if (counter == PACKET_LENGTH) {
                System.out.println("Counter = packetLength");
                PreparedStatement smnt = con.prepareStatement(INSERT_TO_EXP_DATA);
                smnt.setBytes(1, buf);
                smnt.setInt(2, index_cnt++);
                smnt.setInt(3, serID);
                smnt.executeUpdate();
                counter = 0;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void sendLastBuf() {
        try {
            System.out.println("Counter is "  + counter);
            if (counter != 0) {
                byte[] bf = new byte[counter];
                System.arraycopy(buf, 0, bf, 0, bf.length);
                PreparedStatement smnt = con.prepareStatement(INSERT_TO_EXP_DATA);
                smnt.setBytes(1, bf);
                smnt.setInt(2, index_cnt++);
                smnt.setInt(3, serID);
                smnt.executeUpdate();
                counter = 0;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBOutputStream.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
